//
//  CombinedPunchModel.m
//  YuTang
//
//  Created by 葛悦彣 on 2025/3/11.
//

#import "CombinedPunchModel.h"

@implementation CombinedPunchModel
+ (NSDictionary *)modelCustomPropertyMapper {
    return @{
        @"ID":@[@"id"],
    };
}


+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{
        @"tissueList":[CombinedPunchTissueListModel class],
    };
}

-(NSString *)pondNameStr {
    if(!_pondNameStr){
//        if(self.tissueList.count > 1){
//            _pondNameStr = [NSString stringWithFormat:@"%@...",[NSString stringWithFormat:@"%@ %@",self.tissueList.firstObject.tissueName,[self.tissueList.firstObject.pondNameList componentsJoinedByString:@","]]];
//        } else {
            _pondNameStr = [NSString stringWithFormat:@"%@ %@",self.tissueList.firstObject.tissueName,[self.tissueList.firstObject.pondNameList componentsJoinedByString:@","]];
//        }
    }
    return  _pondNameStr;
}
- (BOOL)isHidden{
    return (self.tissueList.count <= 1 && [DTKTool textWithtitle:self.pondNameStr andSize:CGSizeMake(1000, 10000) andFout:kCFont(17)].width < kwt(280));
}
- (CGFloat)viewH{
    if(!self.isHidden){
        if(self.isOpen){
            return kwt(67 + 8) + self.tissueListStrH + kwt(20);
        } else {
            return kwt(74);
        }
    } else {
        return kwt(74);
    }
}
- (NSString *)tissueListStr{
    if(!_tissueListStr){
        NSMutableArray <NSString *>* strArray = [NSMutableArray array];
        for (CombinedPunchTissueListModel * model in self.tissueList) {
            NSString * str = [NSString stringWithFormat:@"%@ (%@)",model.tissueName,[model.pondNameList componentsJoinedByString:@","]];
            [strArray addObject:str];
        }
        _tissueListStr = [strArray componentsJoinedByString:@"\n"];
    }
    return _tissueListStr;
}

- (CGFloat)tissueListStrH{
    CGFloat textH = [DTKTool textWithtitle:self.tissueListStr andSize:CGSizeMake(kwt(315), 10000) andFout:kCFont(15)].height;
    if(textH < kwt(15)){
        return kwt(15);
    }
    return textH;
}
@end

@implementation CombinedPunchTissueListModel

@end

@implementation CombinedPunchDayModel

- (NSMutableArray<CombinedPunchModel *> *)modelArray{
    if(!_modelArray){
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}
-(BOOL)isSeled {
    for (CombinedPunchModel * model in self.modelArray) {
        if(model.isSeled == NO){
            return NO;
        }
    }
    return YES;

}



- (CGFloat)cellH {
    if(self.isSS) {
        return kwt(53);
    } else {
        CGFloat viewH = 0;
        for (CombinedPunchModel * model in self.modelArray) {
            viewH += model.viewH;
        }
        return viewH + kwt(53);

    }
}

- (int)SeledCount{
    int i = 0;
    for (CombinedPunchModel * model in self.modelArray) {
        if(model.isSeled){
            i += 1;
        }
    }
    return i;
}
- (int)NoSeledCount{
    int i = 0;
    for (CombinedPunchModel * model in self.modelArray) {
        if(model.isSeled == NO){
            i += 1;
        }
    }
    return i;
}
@end

@implementation CombinedPunchALLModel
-(NSMutableArray<CombinedPunchDayModel *> *)dayArray{
    if(!_dayArray){
        _dayArray = [NSMutableArray array];
    }
    return _dayArray;
}


- (void)AddModelArray:(NSArray < CombinedPunchModel *>*)array{
    [self.dayArray removeAllObjects];
    CombinedPunchDayModel * daymodel = [CombinedPunchDayModel new];
    for (CombinedPunchModel * model in array) {
        if(![daymodel.executeTime isEqualToString:model.executeTime]){///不同天数
            if(daymodel.executeTime.length > 0){
                [self.dayArray addObject:daymodel];
            }
            daymodel = [CombinedPunchDayModel new];
            daymodel.executeTime = model.executeTime;
            [daymodel.modelArray addObject:model];
        } else {
            [daymodel.modelArray addObject:model];
        }
    }
    if(daymodel.executeTime.length > 0){
        [self.dayArray addObject:daymodel];
    }
}

///当前天数选中
- (BOOL)AddSeledDayModel:(CombinedPunchDayModel *)dayModel {
    ///先判断当前model中药变几个
    if(self.SeledCount + dayModel.NoSeledCount > 50){///超过50个
        return NO;
    }
    for (CombinedPunchModel * model in dayModel.modelArray) {
        model.isSeled = YES;
    }
    return YES;
}

///总共选中了多少个
- (int)SeledCount{
    int i = 0;
    for (CombinedPunchDayModel * model in self.dayArray) {
        i += model.SeledCount;
    }
    return i;
}
- (NSMutableArray<CombinedPunchModel *> *)SeledModelArray{
    NSMutableArray * array = [NSMutableArray array];
    NSMutableDictionary * bzDict = [NSMutableDictionary dictionary];
    ///写入旧数据
    NSMutableArray * oldArray = [NSMutableArray arrayWithArray:self.pondNameList];
    [bzDict setObject:oldArray forKey:self.OldTissueName];
    
    for (CombinedPunchDayModel * daymodel in self.dayArray) {
        for (CombinedPunchModel * model in daymodel.modelArray) {
            if(model.isSeled){
                [array addObject:model];
                if ([[bzDict allKeys] containsObject:model.tissueName]){
                    NSMutableArray * tkArray = bzDict[model.tissueName];
                    NSArray * pondNameArray = [model.pondName componentsSeparatedByString:@","];
                    for (NSString * pondName in pondNameArray) {
                        if(![tkArray containsObject:pondName]){
                            [tkArray addObject:pondName];
                        }
                    }
                   
                } else {
                    NSMutableArray * tkArray = [NSMutableArray array];
                    NSArray * pondNameArray = [model.pondName componentsSeparatedByString:@","];
                    for (NSString * pondName in pondNameArray) {
                        if(![tkArray containsObject:pondName]){
                            [tkArray addObject:pondName];
                        }
                    }
                    [bzDict setObject:tkArray forKey:model.tissueName];
                }
            }
        }
    }
    self.seledDict = bzDict;
    
    return array;
}

@end
